/*******************************************************************************
        Samples\TcpIp\HTTP\static.c
        Z-World, 2000

        A very basic example using the HTTP library. This program
        completely intilizes the library, outputing a basic static
        web page.
*******************************************************************************/
/*
 * 	By default, have compiler make function variables storage class
 * 	"auto" (allocated on the stack).
 */
#class auto


/***********************************
 * Configuration                   *
 * -------------                   *
 * All fields in this section must *
 * be altered to match your local  *
 * network settings.               *
 ***********************************/

/*
 * Pick the predefined TCP/IP configuration for this sample.  See
 * LIB\TCPIP\TCP_CONFIG.LIB for instructions on how to set the
 * configuration.
 */


#define FAT_USE_FORWARDSLASH
#define FAT_BLOCK
#define USE_HTTP_UPLOAD

#define TCPCONFIG 1
#define USE_RABBITWEB 1
#define USE_HTTP_BASIC_AUTHENTICATION 1


/*
 *  The TIMEZONE compiler setting gives the number of hours from
 *  local time to Greenwich Mean Time (GMT).  For pacific standard
 *  time this is -8.  Note:  for the time to be correct it must be set
 *  with tm_rd which is documented in the Dynamic C user manual.
 */

#define TIMEZONE        -8


/********************************
 * End of configuration section *
 ********************************/

/*
 *  memmap forces the code into xmem.  Since the typical stack is larger
 *  than the root memory, this is commonly a desirable setting.  Another
 *  option is to do #memmap anymem 8096 which will force code to xmem when
 *  the compiler notices that it is generating within 8096 bytes of the
 *  end.
 *
 *  #use the Dynamic C TCP/IP stack library and the HTTP application library
 */
#memmap xmem

//#use "fat.lib"
//#use "sflash_fat.lib"


#use "dcrtcp.lib"
#use "http.lib"
#use "userlib.lib"
#web_groups monitor_group, admin_group

/*
 *  ximport is a Dynamic C language feature that takes the binary image
 *  of a file, places it in extended memory on the controller, and
 *  associates a symbol with the physical address on the controller of
 *  the image.
 *
 */

#ximport "samples/tcpip/http/pages/static.html"    index_zhtml
#ximport "samples/tcpip/http/pages/rabbit1.gif"    rabbit1_gif

/*
 *  http_types gives the HTTP server hints about handling incoming
 *  requests.  The server compares the extension of the incoming
 *  request with the http_types list and returns the second field
 *  as the Content-Type field.  The third field defines a custom
 *  function to handle that mime type.
 *
 *  You can get a list of mime types from Netscape's browser in:
 *
 *  Edit->Preferences->Navigator->Applications
 *
 */

/* the default mime type for '/' must be first */
SSPEC_MIMETABLE_START
	SSPEC_MIME_FUNC(".html", "text/html", zhtml_handler),
   SSPEC_MIME(".cgi", ""),
	SSPEC_MIME(".gif", "image/gif")
SSPEC_MIMETABLE_END

/*
 *  The resource table assocates the file image we brought in with ximport
 *  and associates it with its name on the webserver.  In this example
 *  the file "samples/http/pages/static.html" will be sent to the
 *  client when they request either "http://yoururl.com/" or
 *  "http://yoururl.com/index.html"
 *
 */

SSPEC_RESOURCETABLE_START
	SSPEC_RESOURCE_XMEMFILE("/index.html", index_zhtml),
   SSPEC_RESOURCE_CGI("upload.cgi", http_defaultCGI),
	SSPEC_RESOURCE_XMEMFILE("/rabbit1.gif", rabbit1_gif)
SSPEC_RESOURCETABLE_END


int io_state;
#web io_state auth=basic groups=monitor_group(ro),admin_group

void my_io_polling(void);

void main()
{

	/*
	 *  sock_init initializes the TCP/IP stack.
	 *  http_init initializes the web server.
	 *
	 *  sock_init(). This is always mandatory. It initializes the TCP/IP networking system.
	 *  sspec_automount(). This is optional. It initializes the available filesystems (FS2 and/or
	 *  FAT) for use by the resource manager, Zserver.
	 *  http_init(). This is mandatory. It initializes the HTTP server.
	 *
	 */

    int rc;

    sspec_addrule("/index.html", "Pet",
	 admin_group|monitor_group,0, SERVER_HTTP, SERVER_AUTH_BASIC, NULL);

	 sauth_setusermask(sauth_adduser("admin", "123$computer", SERVER_ANY),
	 admin_group, NULL);

	 sauth_setusermask(sauth_adduser("rabbit", "123$computer",SERVER_ANY),
	 monitor_group, NULL);

    rc = sspec_automount(SSPEC_MOUNT_ANY, NULL, NULL, NULL);

    if (rc)
		printf("Failed to initialize, rc=%d\n Proceeding anyway...\n", rc);




   sock_init();
   http_init();

   /*
    send_notification("sydneydixon@yahoo.com");
    send_notification("mmartin101@gmail.com");
    send_notification("rbrtflores73@gmail.com");
    */

	/*
	 *  tcp_reserveport causes the web server to ignore requests when there
	 *  isn't an available socket (HTTP_MAXSERVERS are all serving index_html
	 *  or rabbit1.gif).  This saves some memory, but can cause the client
	 *  delays when retrieving pages.
	 */

   tcp_reserveport(80);

	/*
	 *  http_handler needs to be called to handle the active http servers.
	 */


   for (;;) {
   	my_io_polling();
      http_handler();
   }
}

	void my_io_polling()
	{
		io_state = 50; //read_that_io_device();
      //io_state = read_that_io_device();
	}